package cn.qicaiwang.open.admin.controller.plans;

import cn.qicaiwang.open.app.entity.MembershipPlan;
import cn.qicaiwang.open.app.server.MembershipPlanService;
import cn.qicaiwang.open.common.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 后台会员套餐控制器
 */
@RestController
@RequestMapping("/plans")
public class AdminMembershipPlanController {
    @Autowired
    private MembershipPlanService membershipPlanService;
    /**
     * 创建会员套餐
     * @param plan 会员套餐实体
     * @return 创建的会员套餐
     */
    @PostMapping
    public ResponseResult<MembershipPlan> createPlan(@RequestBody MembershipPlan plan) {
        MembershipPlan createdPlan = membershipPlanService.createPlan(plan);
        return ResponseResult.ok(createdPlan);
    }

    /**
     * 更新会员套餐
     * @param plan 会员套餐实体
     * @return 更新后的会员套餐
     */
    @PostMapping("/update")
    public ResponseResult<MembershipPlan> updatePlan(@RequestBody MembershipPlan plan) {
        MembershipPlan updatedPlan = membershipPlanService.updatePlan(plan);
        return ResponseResult.ok(updatedPlan);
    }


    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> deletePlan(@PathVariable Long id) {
        boolean result = membershipPlanService.removeById(id);
        return result ? ResponseResult.ok(result) : ResponseResult.error("删除失败");
    }

    /**
     * 获取会员套餐详情
     * @param id 套餐ID
     * @return 会员套餐实体
     */
    @GetMapping("/{id}")
    public ResponseResult<MembershipPlan> getPlanById(@PathVariable Long id) {
        MembershipPlan plan = membershipPlanService.getPlanById(id);
        return ResponseResult.ok(plan);
    }

    /**
     * 获取所有会员套餐
     * @return 会员套餐列表
     */
    @GetMapping
    public ResponseResult<List<MembershipPlan>> getAllPlans() {
        List<MembershipPlan> plans = membershipPlanService.getAllPlans();
        return ResponseResult.ok(plans);
    }

    /**
     * 获取所有启用的会员套餐
     * @return 启用的会员套餐列表
     */
    @GetMapping("/active")
    public ResponseResult<List<MembershipPlan>> getActivePlans() {
        List<MembershipPlan> plans = membershipPlanService.getActivePlans();
        return ResponseResult.ok(plans);
    }
}

